//
//  LoadingPageView.swift
//  sss
//
//  Created by MAC on 2020/12/24.
//  Copyright © 2020 hrrMac. All rights reserved.
//

import UIKit

class LoadingPageView: UIView {
    
//    设置图片的宽高，一般来说是一个定值
    private static let picW = 200
    private static let picH = 200
    
    private let imgView  = UIImageView()
    private let titleLab = UILabel()
    private let btn      = UIButton()
    
    /// 弹出位置的容器视图
//    private weak var containerView: UIView?
    
    //外部设置的偏移量 （负数向上偏移  正数向下偏移）
    var offset: Int?
    //按钮的点击事件
    var clickBtnEvent: (()->Void)?
    
    //内部私有的偏移量，来控制视图更加好的便宜
    private var priOffset: Int = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    convenience init(_ containerView: UIView) {
        self.init(frame:containerView.bounds)
        containerView.addSubview(self)

        self.translatesAutoresizingMaskIntoConstraints = false
        self.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0).isActive = true
        self.rightAnchor.constraint(equalTo: containerView.rightAnchor, constant: 0).isActive = true
        self.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0).isActive = true
        self.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 0).isActive = true
    }
    
    
    private func configUI(){
        self.backgroundColor = UIColor.white
        
        addSubview(imgView)
        addSubview(titleLab)
        addSubview(btn)
        
        imgView.contentMode    = .scaleAspectFill
        imgView.clipsToBounds  = true
        
        titleLab.textAlignment = .center
        titleLab.textColor     = UIColor(r: 153, g: 153, b: 153)
        titleLab.font          = UIFont.systemFont(ofSize: 15)
        
        btn.setTitleColor(UIColor(r: 59, g: 146, b: 147), for: .normal)
        btn.titleLabel?.font   = UIFont.systemFont(ofSize: 14)
        btn.layer.cornerRadius = 3
        btn.layer.borderWidth  = 1
        btn.layer.borderColor  = UIColor(r: 59, g: 146, b: 147).cgColor
        btn.addTarget(self, action: #selector(btnClickEvent), for: .touchUpInside)
    }
    
    @objc func btnClickEvent(){
        if clickBtnEvent != nil {
            clickBtnEvent!()
        }
    }
    
    //MARK: - 内部配置
    ///设置尺寸
    private func viewCombination(){
        
        let h = self.frame.size.height
        let w = self.frame.size.width
        
        let offset = self.offset ?? 0 + priOffset
        
        imgView.frame = CGRect(x: (Int(w) - LoadingPageView.picW) / 2,
                               y: Int(2 / 4.0 * h) - LoadingPageView.picH + offset,
                               width: LoadingPageView.picW,
                               height: LoadingPageView.picH)
        
        titleLab.frame = CGRect(x: 10, y: Int(2 / 4.0 * h) + 20 + offset, width: Int(w) - 20, height: 30)
        
        btn.frame = CGRect(x: (Int(w) - 80) / 2, y: Int(titleLab.frame.origin.y) + 30 + offset, width: 80, height: 26)
    }
    
    ///设置值
    private func configValue(_ imgStr: String , title: String?, btnStr: String?){
        imgView.image = UIImage(named: imgStr)
        titleLab.text = title
        if btnStr != nil {
            btn.setTitle(btnStr, for: .normal)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        print(#function + debugDescription)
        viewCombination()
    }
    
}


extension LoadingPageView{
        
    /// 加载挡板
    func showLoadingPage(){
        self.alpha = 1
        self.titleLab.isHidden = true
        self.btn.isHidden = true
        self.isHidden = false
        configValue("empty", title: "", btnStr: nil)
        priOffset = LoadingPageView.picH / 2
        viewCombination()
    }
    
    
    func dismissLoadingPage(){
        self.alpha = 0
        self.isHidden = true
    }
    
    
    func emptyView(){
        
    }
}


///提供一个公共的方法可以随时调用，可以通过key来获取到对象
//@discardableResult
//public func showBaffle(_ containerView: UIView, keyName: ClassType) -> LoadingPageView{
//    return nil;
//}

